# -*- coding: utf-8 -*-
import sys
import logging
import traceback

import py_global

logger = logging.getLogger(__name__)

"""
异常
Exception

错误
KeyError
TypeError
ValueError
IOError



__cause__
__context__
__traceback__

"""


def py01():
    try:
        message = 'message'
        raise Exception(message)
    except Exception as e:
        # 获取异常信息文本
        exc_type, exc_value, exc_traceback = sys.exc_info()
        tb_lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
        tb_str = ''.join(tb_lines)
        print(tb_str)

        # logging.error(e)
        # 显示异常详情
        # logging.error(f'error:{e}', exc_info=True)
        # logging.error(f'error:{repr(e)},{e}', exc_info=True)
        # logging.error('%s:%s', 'error', e, exc_info=True)
        # logging.error('%s:%s', 'error', e, exc_info=e)
        # logging.exception(e)
        # 显示堆栈详情
        # logging.exception(f'error:{e}', stack_info=True)

        # 已有堆栈详情：Traceback
        # logger.exception(f'error:{repr(e)}')
        # logger.exception('error:%s', repr(e))
        # logger.exception('error:%s', e)

        # 错误示例：TypeError: not all arguments converted during string formatting
        # logger.error('error:', repr(e))
        # logger.error('error:', e)

        # logging.debug(str(e))
        # logging.debug(repr(e))

        # raise e

    pass


class ExtendsException(Exception):

    def __init__(self):
        # self.args属性
        self.args = ('',)
        pass

    pass


def py02():
    try:
        print('try')
        # raise Exception('message')
        pass
    except Exception as e:
        print('except')
        # 链式异常
        raise ExtendsException() from e
        pass
    else:
        print('else')
        pass
    finally:
        print('finally')
        pass

    pass


if __name__ == '__main__':
    py01()
    # py02()

    pass
